1
namespace Org
.Lwes
.Tests
5 using System
.Threading
;
7 using Microsoft
.VisualStudio
.TestTools
.UnitTesting
;
11 using Org
.Lwes
.Emitter
;
12 using Org
.Lwes
.Listener
;
15 public class EventListenerTests
21 public void AccessDefaultListener()
25 EventName
= "UserLogin",
28 UserName
= new { Name = "username", Token = TypeToken.STRING, Value = "bob" }
,
29 Password
= new { Name = "password", Token = TypeToken.UINT64, Value = 0xfeedabbadeadbeefUL }
,
30 ClientIP
= new { Name = "clientIP", Token = TypeToken.IP_ADDR, Value = IPAddress.Parse("127.0.0.1") }
,
31 Successful
= new { Name = "successful", Token = TypeToken.BOOLEAN, Value = false }
35 Event receivedEvent
= default(Event
);
37 // Mock an IEventSink that records the incomming event...
38 Mock
<IEventSink
> mock
= new Mock
<IEventSink
>();
39 mock
.Setup(framework
=> framework
.HandleEventArrival(It
.IsAny
<ISinkRegistrationKey
>(), It
.IsAny
<Event
>()))
40 .Callback
<ISinkRegistrationKey
, Event
>((k
, ev
) =>
47 using (IEventListener listener
= EventListener
.CreateDefault())
49 Assert
.IsNotNull(listener
);
50 listener
.RegisterEventSink(mock
.Object
).Activate();
52 IEventEmitter emitter
= EventEmitter
.CreateDefault();
53 Assert
.IsNotNull(emitter
);
55 // Create the event...
56 sourceEvent
= new Event(e
.EventName
);
57 sourceEvent
.SetValue(e
.Attributes
.UserName
.Name
, e
.Attributes
.UserName
.Value
);
58 sourceEvent
.SetValue(e
.Attributes
.Password
.Name
, e
.Attributes
.Password
.Value
);
59 sourceEvent
.SetValue(e
.Attributes
.ClientIP
.Name
, e
.Attributes
.ClientIP
.Value
);
60 sourceEvent
.SetValue(e
.Attributes
.Successful
.Name
, e
.Attributes
.Successful
.Value
);
62 // give the round-trip 20 seconds to complete, this is extremely generous...
63 long time_out_ticks
= DateTime
.Now
.Ticks
+ TimeSpan
.FromSeconds(20).Ticks
;
65 while (!done
&& DateTime
.Now
.Ticks
< time_out_ticks
)
67 emitter
.Emit(sourceEvent
);
71 Assert
.IsTrue(done
, "Should have received an event");
72 Assert
.AreEqual(sourceEvent
[e
.Attributes
.UserName
.Name
].GetValue
<string>(), receivedEvent
[e
.Attributes
.UserName
.Name
].GetValue
<string>());
73 Assert
.AreEqual(sourceEvent
[e
.Attributes
.Password
.Name
].GetValue
<ulong>(), receivedEvent
[e
.Attributes
.Password
.Name
].GetValue
<ulong>());
74 Assert
.AreEqual(sourceEvent
[e
.Attributes
.ClientIP
.Name
].GetValue
<IPAddress
>(), receivedEvent
[e
.Attributes
.ClientIP
.Name
].GetValue
<IPAddress
>());
75 Assert
.AreEqual(sourceEvent
[e
.Attributes
.Successful
.Name
].GetValue
<bool>(), receivedEvent
[e
.Attributes
.Successful
.Name
].GetValue
<bool>());
76 Console
.Write(receivedEvent
.ToString());